// pages/classify/index.js
import cookie from '../../utils/js-cookie'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search_history_show: true,
    search_history: [],
    tab: {
      book_lists: [],
      beginrow: 0,
      islastline: 0,
      update: false,
      loading: false
    },
    name: ''//搜索条件
  },
  cancel() {
    let tab = {
      book_lists: [],
      beginrow: 0,
      islastline: 0,
      update: false,
      loading: false
    }
    this.setData({ name: '', tab: tab })
  },
  bindfocus() {
    this.setData({ search_history_show: true })
  },
  search(name, beginrow, addSearchType = true) {
    let self = this
    let tab = self.data.tab
    console.log(tab)
    wx.showLoading({
      title: 'loading..',
    })
    app.getToken(token => {
      let data = {
        token: token,
        name: name,
        beginrow: beginrow,
        pagecount: 12
      }
      if (!!data.name && addSearchType) {
        self.data.search_history.push(data.name)
        self.setData({ 'search_history': self.data.search_history }, () => {
          cookie.setCookie('search_history_tt', self.data.search_history)
        })
      }
      app.getApi.searchbook(data).then(res => {
        wx.hideLoading()
        if (res.code == 3000) {
          if (beginrow == 0) { //初始化
            tab.book_lists = res.data
            tab.update = true
          } else {
            tab.book_lists = tab.book_lists.concat(res.data)
          }
          tab.islastline = res.islastline
          tab.beginrow = beginrow
          tab.loading = false
          self.setData({
            tab: tab,
            search_history_show: false,
            name: name
          })
        }
      }).catch(Error => {
        console.log(Error)
      })
    })
  },
  searchDone() {
    let self = this
    let name = self.data.name
    let addSearchType = true
    self.search(name, 0, addSearchType)
  },
  confirmSearch(e) {
    let name, self = this
    let addSearchType
    name = e.currentTarget.dataset.tit
    addSearchType = false
    self.search(name, 0, addSearchType)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    self.getHistory()
  },
  getHistory() {
    let self = this
    cookie.getCookie('search_history_tt', (res) => {
      console.log(res)
      self.setData({
        search_history: res
      })
    }, () => {
      console.log('cookie失败', )
    })
  },
  deleteHistory(e) {
    let self = this
    console.log(e)
    let index = e.currentTarget.dataset.index
    self.data.search_history.splice(index, 1)
    self.setData({
      search_history: self.data.search_history
    }, () => {
      cookie.setCookie('search_history_tt', self.data.search_history)
    })
  },
  goDetail(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../detail/index?id=${id}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this
    let tab = self.data.tab
    if (tab.loading || tab.islastline) { //是否到底(0：否，1：是)
      return
    }
    this.setData({
      [`tab.loading`]: true
    }, () => {
      let beginrow = self.data.tab[`beginrow`]
      setTimeout(() => { self.search(self.data.name, beginrow + 12) }, 500)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '天天享读，让图书传递爱',
      imageUrl: '../../assets/images/index_s.png',
      path: '/pages/index/index'
    }
  }
})